<?php
/*
 * @ author - atul rajguru (atulrajguru9@gmail.com)
 *
 * Version 1.00 Thursday, August 12, 2010 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
 
set_time_limit(0);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

require_once ('lib/SforcePartnerClient.php');
require_once ('lib/SforceHeaderOptions.php');
require_once ('lib/config.php');

if (strlen($user) < 1)
{
	echo 'Error : User Name ($user) is not set in soapclient/config.php';
	exit(0);
}
if (strlen($pass) < 1)
{
	echo 'Error : Password ($pass) is not set in soapclient/config.php';
	exit(0);
}


try
{
	$wsdl = 'lib/partner.wsdl.xml';

	//connect to salesforce
	$client = new SforcePartnerClient();
	$client->createConnection($wsdl);
	$loginResult = $client->login($user, $pass.$token);

	if (isset($client))
	{
		// Retrieve all the objects in the organization
		$result = $client->describeGlobal();

		// Cycle through each object
		foreach ($result->types as $objectType)
		{		
			if(in_array($objectType, $objects_cant_query))
				continue;
			
			if($objects_to_check != "ALL")
			if(!in_array($objectType, $objects_to_check))
				continue;

			$query = "Select id from ".$objectType;

			$resultObject = $client->query($query);
			
			$total_count = $resultObject->size;
		
			// Retrieve the object definition
			$resultObject = $client->describeSObject($objectType);
		
			echo "\n\n\n\n Analysing Object : ". $objectType;
			echo "\n ".str_repeat("=", 70 );
			echo "\n Field Name " . str_repeat(" ", 39) . ": % Used ";
			echo "\n ".str_repeat("-", 70 );
			$stats = array();
			// Cycle through each field
			if($total_count > 0)
			{
				foreach ($resultObject->fields as $fieldObj)
				{
					//print_r($fieldObj);
					if(substr($fieldObj->name, -3) != "__c")
					continue;

					$field = $fieldObj->name;
					$count = 0;

					if($fieldObj->type == "textarea")
					{
						$query = "Select ".$field ." from ".$objectType;
						$resultObjectData = $client->query($query);	
					
						//print_r($resultObjectData);
						//die;

						foreach ($resultObjectData->records as $Obj)
						{
							if($Obj->fields->$field == "")
							{
								$count += 1;
							}
						}
					}
					else
					{
						$query = "Select ".$field ." from ".$objectType. " where ". $field ." = Null ";
						$resultObject = $client->query($query);					
						$count = $resultObject->size;
					}
					if($fieldObj->defaultedOnCreate == 1)
						$def = " ** default is set";
					else
						$def = "";
					$stats[$field . $def] = $count;
				}
				
				if(count($stats) > 0)
				{
					asort($stats,SORT_NUMERIC);										
									
					foreach ($stats as $field => $count)
					{
						echo "\n\n $field" . str_repeat(" ", 50 - strlen($field) ) . ": " . round(($count / $total_count * 100),2) . "% ($count/$total_count)";
					}
				}
				else
				{
					echo "\n\n There are no Custom Fileds in $objectType";
				}
			}
			else
			{
				echo "\n\n There are no records in $objectType";
			}
			echo "\n\n";
		}
	}
}
catch (exception $e)
{
	// This is reached if there is a major problem in the data or with
	// the salesforce.com connection. Normal data errors are caught by
	// salesforce.com
	echo '<pre>' . print_r($e, true) . '</pre>';
	return false;
	exit;
}

